import {applicationFormUrlEncodedHeader, applicationJsonHeader, doGet, doPost} from "@/net/axios-request.ts";
import {userTokenHeader} from "@/utils/auth-utils.ts";

export interface Role {
    roleId: number,
    name: string
}

export function getEmptyRole(): Role {
    return {name: "", roleId: 0}
}

export async function getAllRoles() {
    return (await doGet<Role[]>("/api/rbac/listAllRoles", {...userTokenHeader()}, {})).data
}

export async function deleteRole(roleId: number) {
    return await doPost("/api/rbac/deleteRole", {...userTokenHeader(), ...applicationFormUrlEncodedHeader}, { roleId: roleId })
}

export async function saveOrUpdateRole(role: Role) {
    return await doPost("/api/rbac/saveRole", {...userTokenHeader(), ...applicationJsonHeader}, {...role})
}

export interface Permission {
    permissionId: number,
    name: string
}

export function getEmptyPermission(): Permission {
    return {name: "", permissionId: 0}
}

export async function getAllPermissions() {
    return (await doGet<Permission[]>("/api/rbac/listAllPermissions", {...userTokenHeader()}, {})).data
}

export async function deletePermission(permissionId: number) {
    return await doPost("/api/rbac/deletePermission", {...userTokenHeader(), ...applicationFormUrlEncodedHeader}, { permissionId: permissionId })
}

export async function saveOrUpdatePermission(permission: Permission) {
    return await doPost("/api/rbac/savePermission", {...userTokenHeader(), ...applicationJsonHeader}, {...permission})
}

export async function getRolePermissions(roleId: number) {
    return await doGet<Permission[]>("/api/rbac/getRolePermissions", {...userTokenHeader()}, { roleId: roleId })
}

export async function bindRolePermissions(roleId: number, permissionId: number) {
    return await doPost("/api/rbac/bindRolePermissions", {...userTokenHeader(), ...applicationFormUrlEncodedHeader}, { roleId: roleId, permissionId: permissionId })
}

export async function unbindRolePermissions(roleId: number, permissionId: number) {
    return await doPost("/api/rbac/unbindRolePermissions", {...userTokenHeader(), ...applicationFormUrlEncodedHeader}, { roleId: roleId, permissionId: permissionId })
}